{include file="public/header" /}
<body class="gray-bg">
<div class="wrapper wrapper-content">
    <div class="ibox float-e-margins">
        <div class="ibox-content">
            <div class="row">
                <!--搜索框开始-->
                <div class="col-sm-12">
                    <div class="layui-form layui-form-item">
                        <div class="layui-col-md6 layui-input-inline">
                            <select name="coupon_status" id="search1" lay-search="" class="layui-input">
                                <option  value="">全部类型</option>
                                <option  value="1">上架</option>
                                <option  value="2">到期</option>
                                <option  value="3">下架</option>
                            </select>
                        </div>
                        <div class="layui-col-md6 layui-input-inline">
                            <input type="text" id="key" class="layui-input search" name="key" value="" placeholder="优惠卷名字/商家名称"/>
                        </div>
                        <div class="layui-input-inline">
                            <button class="layui-btn" lay-submit="" lay-filter="LAY-search">立即搜索</button>
                            <button  class="layui-btn layui-btn-normal" id="empty"  lay-submit="" lay-filter="LAY-search">重置</button>
                        </div>
                        <button  class="layui-btn layui-btn-normal" style="float: right;" onclick="daochu();">导出</button>
                    </div>
                </div>
                <!--搜索框结束-->
                <!--操作框开始-->
                <!--操作框结束-->
            </div>
            <div class="hr-line-dashed"></div>

            <div class="example-wrap">
                <div class="example">
                    <table class="layui-hide" id="test" lay-filter="demo"></table>
                    <script type="text/html" id="imgTpl">
                        <img src="{{d.coupon_image}}" class="layui-table-img layui-cricle" onerror="this.src='/static/admin/images/head_default.gif'"/>
                    </script>

                    <script type="text/html" id="coupon_status">
                        {{# if(d.coupon_status ==1){ }}
                        上架
                        {{# } }}
                        {{# if(d.coupon_status ==2){ }}
                        到期
                        {{# } }}
                        {{# if(d.coupon_status ==3){ }}
                        下架
                        {{# } }}
                    </script>
                    <script type="text/html" id="coupon_start">
                       {{d.coupon_start}}/{{d.coupon_end}}
                    </script>
                    <!--操作模板-->
                    <script type="text/html" id="opeBar">
                        <a href="javascript:;" onclick="wk.href('{:url('detail')}?id={{d.coupon_id}}')" class="btn btn-primary btn-xs m-r-xs">领取详情</a>
                        {{# if(d.coupon_status==1){ }}
                        <a href="javascript:;" onclick="wk.confirm('{{d.coupon_id}}','{:url('lower')}','确定要下架吗?')" class="btn btn-danger btn-xs m-r-xs">下架</a>
                        {{# } }}
                        {{# if(d.coupon_status!=1){ }}
                        <a href="javascript:;" onclick="wk.confirm('{{d.coupon_id}}','{:url('del')}','确定要删除吗?')" class="btn btn-danger btn-xs m-r-xs">删除</a>
                        {{# } }}
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<input type="hidden" id="s_id" value="{$s_id}" />
{include file="public/footer" /}
<script>
    var s_id = $('#s_id').val();
    //layui渲染数据表格
    layui.use('table', function () {

        var table = layui.table
                , form = layui.form;
        table.render({
            elem: '#test'
            , url: '{:url("coupon/index")}'
            ,response: {
                statusCode: 220 //成功的状态码，默认：0
            }
            , page: true
            , size: 'lg' //sm小尺寸的表格 lg大尺寸
            , cellMinWidth: 150
            , limits: [5, 10, 15, 20, 25, 30]
            , where: {s_id:s_id}
            , limit: "{:config('pages')}"
            , loading: true
            , id: 'testReload'
            , cols: [[
                {type:'numbers', width: 70, title: '序号'}
                , {field: 'coupon_id', width: 50, title: 'ID', align: 'center'}
                , {field: 's_name', width: '', title: '商户名字', align: 'center'}
                , {field: 's_phone', width: '', title: '商户电话', align: 'center'}
                , {field: 'coupon_status', width: '', title: '优惠券状态', align: 'center',templet:'#coupon_status'}
                , {field: 'coupon_name', width: '', title: '优惠券名称', align: 'center'}
                , {field: 'coupon_image', width: '', title: '优惠券图片', align: 'center',templet:'#imgTpl'}
                , {field: 'coupon_price', width: 90, title: '兑换金币', align: 'center'}
                , {field: 'coupon_money', width: 70, title: '面额', align: 'center'}
                , {field: 'coupon_num', width: 70, title: '总数', align: 'center'}
                , {field: 'coupon_get_num', width: 90, title: '领取数', align: 'center'}
                , {field: 'coupon_use_num', width: 90, title: '核销数', align: 'center'}
                , {field: 'coupon_start', width: '200', title: '领取有效期--起止日期', align: 'center',templet:'#coupon_start'}
                , {field: 'coupon_use_day', width: '', title: '使用有效期/天', align: 'center'}
                , {field: 'coupon_time', width: '', title: '发布时间', align: 'center'}
                , {fixed: 'right', width:350, title: '操作', align: 'center', toolbar: '#opeBar'}
            ]]
            ,done: function (res, curr, count) {
                $('.layui-cricle').viewer();//图片查看器
            }
        });
        //监听状态开关操作
        form.on('switch(OnOff)', function (obj) {
            var num = '';
            obj.elem.checked == true? num = 1: num = 2;
            //用户状态
            wk.status(this.value,num, '{:url("memberState")}',obj);
        });

        //批量操作
        var $ = layui.$, active = {
            excelAll: function(){
                //导出Excel
                wk.excelAll(getIds(),$('.layui-input').serialize(),"{:url('excelMember')}");
            }
        };
        $('.layuiBtn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        var getIds = function () {
            var ids = [];
            var checkStatus = table.checkStatus('testReload')
                    ,data = checkStatus.data;
            $.each(data,function(index,item){
                ids.push(item['task_id'])
            });
            return ids;
        }
    });
    function daochu() {
        var name = $('#key').val().trim();
        var type = $('#search1').val().trim();
        var s_id = $('#s_id').val().trim();
        $.ajax({
            url: '/admin/Coupon/excelCoupon',
            data: 'name=' + name + '&type=' + type + '&s_id=' + s_id,
            type: 'POST',
            dataType: 'JSON',
            success: function (returnData) {
                if(returnData.code == 200){
                    location.href = (returnData.msg);
                }else{
                    alert(returnData.msg);
                }
            },
            error: function () {
                alert('数据出错，请刷新重试');
            }
        })
    }
</script>
</body>
</html>